#include "filestream.h"

FileStream::FileStream(const string& filename, iomode mode):
DataStream(filename, mode)
{
    if (mode == Input)
        mFileStr.open(filename.c_str(), fstream::in | fstream::binary);
    else if (mode == Output)
        mFileStr.open(filename.c_str(), fstream::out | fstream::binary);
    if(mFileStr.fail())
        cerr << "FileStream(filename, mode) : Could not open " << filename << endl;
}

FileStream::~FileStream()
{
    mFileStr.close();
}

bool FileStream::read(void* data, size_t size)
{
    assert(data);

    mFileStr.read((char*)data, size);
    return !mFileStr.fail();
}

int FileStream::getReadOffset()
{
    return mFileStr.tellg();
}

bool FileStream::setReadOffset(int offset, ios_base::seekdir origin)
{
    mFileStr.seekg(offset, origin);
    return !mFileStr.fail();
}

bool FileStream::write(const void* data, size_t size)
{
    assert(data);

    mFileStr.write((const char*)data, size);
    return !mFileStr.fail();
}

int FileStream::getWriteOffset()
{
    return mFileStr.tellp();
}

bool FileStream::setWriteOffset(int offset, ios_base::seekdir origin)
{
    mFileStr.seekp(offset, origin);
    return mFileStr.fail();
}
